<?php
class User
{
	public $userID;
	public $info;

	public function __construct()
	{
		$this->userID = null;
		$this->info = array(
			'userName' => '',
			'userPass' => '',
			'userRName' => '',
			'userSex' => '',
			'userPhone' => '',
			'userProGrade' => '',
			'userMail' => ''
			);
	}

	public static function GetUserByID($userID)
	{
		$user = new User();
		$sql = "select * from lib_user where lib_user_id = $userID";
		$result = mysqli_query($GLOBALS['DB'], $sql);
		$row = mysqli_fetch_assoc($result);

		if(mysqli_num_rows($result) > 0){

			$user->userID = $row['lib_user_id'];
			$user->info['userName'] = $row['lib_user_name'];
			$user->info['userPass'] = $row['lib_user_pass'];
			$user->info['userRName'] = $row['lib_user_Rname'];
			$user->info['userSex'] = $row['lib_user_sex'];
			$user->info['userPhone'] = $row['lib_user_phone'];
			$user->info['userProGrade'] = $row['lib_user_proGrade'];
			$user->info['userMail'] = $row['lib_user_mail'];

			mysqli_free_result($result);
			return $user;
		}else{
			return false;
		}
	}

	public function SaveUser()
	{
		$sUserID = $this->userID;
		$sUserName = $this->info['userName'];
		$sUserPass = $this->info['userPass'];
		$sUserRName = $this->info['userRName'];
		$sUserSex = $this->info['userSex'];
		$sUserPhone = $this->info['userPhone'];
		$sUserProGrade = $this->info['userProGrade'];
		$sUserMail = $this->info['userMail'];

		if($this->userID){

			$sql = "update lib_user set
				lib_user_id = '$sUserID', 
				lib_user_name = '$sUserName',
				lib_user_pass = '$sUserPass', 
				lib_user_Rname = '$sUserRName',
				lib_user_sex = '$sUserSex', 
				lib_user_phone = '$sUserPhone',
				lib_user_proGrade = '$sUserProGrade', 
				lib_user_mail = '$sUserMail'
				where
				lib_user_id = '$sUserID'";

			if(mysqli_query($GLOBALS['DB'], $sql)){
				return true;
			}else{
				return false;
			}

		}else{
			$sql = "insert into lib_user values(
				NULL,
				'$sUserName',
				'$sUserPass',
				'$sUserRName',
				'$sUserSex',
				'$sUserPhone',
				'$sUserProGrade',
				'$sUserMail'
				)";
			if(mysqli_query($GLOBALS['DB'], $sql)){
				$this->userID = mysqli_insert_id($GLOBALS['DB']);
				return $this->userID;
			}else{
				return false;
			}
		}
	}
}
?>